<?php
namespace Modules\Home\Validations;

use Phalcon\Validation\Validator\PresenceOf;

use Common\Models\UserShopModel;

class UserShopValidation extends \Phalcon\Validation {
  public function create($post) {
    $this -> add('real_name',
      new PresenceOf([
        'message' => '请输入真实姓名!'
      ])
    );

    $this -> add('shop_level',
      new PresenceOf([
        'message' => '请选择店铺等级!'
      ])
    );

    $this -> add('telphone',
      new PresenceOf([
        'message' => '请输入电话!'
      ])
    );

    $this -> add('province_code',
      new PresenceOf([
        'message' => '请选择省!'
      ])
    );
    $this -> add('city_code',
      new PresenceOf([
        'message' => '请选择市!'
      ])
    );
    $this -> add('district_code',
      new PresenceOf([
        'message' => '请选择区!'
      ])
    );

    $this -> add('address_detail',
      new PresenceOf([
        'message' => '请输入详细地址!'
      ])
    );

    $validate = $this -> validate($post);
    if($validate -> valid()) {
      return ['code' => -1, 'message' => $validate[0] -> getMessage()];
    }

    if(UserShopModel::findFirst('user_id = ' . $post['user_id'])) {
      return ['code' => -1, 'message' => '已成功申请，等待审核!'];
    }

    if(!empty($post['district_code'])) {
      $post['address_code'] = $post['district_code'];
    } else if(!empty($post['city_code'])) {
      $post['address_code'] = $post['city_code'];
    } else if(!empty($post['province_code'])) {
      $post['address_code'] = $post['province_code'];
    } else {
      $post['address_code'] = 0;
    }

    unset($post['district_code']);
    unset($post['city_code']);
    unset($post['province_code']);

    $post['create_time'] = time();
    $post['status'] = 1;
    return ['code'=>0,'post'=>$post];
  }
}
?>